package com.kandinfo.isbnmanage.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kandinfo.base.common.converter.ConverterService;
import com.kandinfo.base.common.service.AbstractService;
import com.kandinfo.base.security.SecurityContext;
import com.kandinfo.isbnmanage.domian.IsbnDistribute;
import com.kandinfo.isbnmanage.domian.IsbnInformation;
import com.kandinfo.isbnmanage.domian.IsbnManage;
import com.kandinfo.isbnmanage.dto.IsbnInformationDto;
import com.kandinfo.isbnmanage.repository.IsbnDistributeRepository;
import com.kandinfo.isbnmanage.repository.IsbnInformationRepository;
import com.kandinfo.isbnmanage.repository.IsbnManageRepository;
import com.kandinfo.isbnmanage.service.IsbnDistributeService;
import com.kandinfo.isbnmanage.service.IsbnInformationService;
import com.kandinfo.isbnmanage.service.IsbnManageService;



@Service
public class IsbnInformationServiceImpl extends AbstractService<IsbnInformation, Long, IsbnInformationRepository> implements IsbnInformationService {

	@Autowired
	IsbnInformationRepository isbnInformationRepository;
	
	@Autowired
	IsbnManageRepository isbnManageRepository;
	
	@Autowired
	IsbnDistributeRepository isbnDistributeRepository;
	
	@Autowired
	IsbnDistributeService isbnDistributeService;
	
	@Autowired
	IsbnManageService isbnManageService;
	
	@Resource
    public void setIsbnInformationRepository(
    		IsbnInformationRepository isbnInformationRepository){
        super.setRepository(isbnInformationRepository);
    }
	
	@Override
	public List<IsbnInformation> findByBarcode(String barcode){
		return isbnInformationRepository.findByBarcode(barcode);
	}

	@Override
	public List<IsbnInformation> findByBarcodeAndInforId(String barcode, Long isbnInformationId) {
		if (isbnInformationId == null) {
			return isbnInformationRepository.findByBarcode(barcode);
		} else {
			return isbnInformationRepository.findByBarcodeAndInforId(barcode, isbnInformationId);
		}
	}

	@Override
	public IsbnInformation findInforByInforId(Long isbnInformationId) {
		return isbnInformationRepository.findInforByInforId(isbnInformationId);
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public IsbnInformationDto newInformation(IsbnInformationDto isbnInformationResDto) throws Exception {
		Date date = new Date();
		Long curUserId = SecurityContext.getInstance().getCurrentUserId();
		
		IsbnInformation isbnInformation = ConverterService.convert(isbnInformationResDto, IsbnInformation.class);
		isbnInformation.setStatus(0);
		isbnInformation.setCreateBy(curUserId);
		isbnInformation.setCreateTs(date);
		isbnInformationRepository.save(isbnInformation);
		
		String barcode = isbnInformation.getBarcode();
		if (StringUtils.isEmpty(barcode)) {
			throw new Exception("条形码信息为空！");
		}
		IsbnManage isbnManage = isbnManageRepository.getByBarcode(barcode);
		if (isbnManage == null) {
			throw new Exception("isbnManage信息不存在！");
		}
		
		//如果作业本是新建状态  对应购书任务状态应该为未完成
		IsbnDistribute isbnDistribute = isbnDistributeRepository.findByBarcodeAndType(barcode,0);
		if (isbnDistribute ==null) {
			throw new Exception("条形码【"+barcode+"】对应分发任务不存在");
		}
		
		if (0 != isbnDistribute.getStatus()) {
			isbnDistribute.setStatus(0);
			isbnDistribute.setUpdateTs(date);
			isbnDistribute.setUpdateBy(curUserId);
			isbnDistributeService.save(isbnDistribute);
		}
		
		//统计isbnInformation数量
		Integer count = isbnInformationRepository.getCountByBarcode(barcode);
		isbnManage.setIsbnInformationCount(count);
		isbnManage.setUpdateBy(curUserId);
		isbnManage.setUpdateTs(date);
		isbnManageService.save(isbnManage);
		
		isbnInformationResDto = ConverterService.convert(isbnInformation, IsbnInformationDto.class);
		return isbnInformationResDto;
	}

}
